function [ beta ] = findBeta2( U_p, U_s, I_s, cos_phi, f_s, L_d, L_q, L_so, R_s, Schranke_cos, Schranke_U)
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
beta=pi/2;
omega=2*pi*f_s;

%  U_p
%  U_s
%  I_s
%  cos_phi
%  f_s
%  L_d
%  L_q
%  L_so
%  R_s
%  Schranke_cos
%  Schranke_U



I_ges=I_s*exp(j*beta);
I_q=j*imag(I_s*exp(j*beta));
I_d=real(I_s*exp(j*beta));
XqhIsq=j*omega*(L_q-L_so)*I_q;
XdhIsd=j*omega*(L_d-L_so)*I_d;
XsoIs=j*omega*L_so*I_ges;
RsIs=R_s*I_ges;

U_akt=j*U_p+XqhIsq+XdhIsd+XsoIs+RsIs;
U_abs=abs(U_akt);
cos_phi_aktuell=cos(angle(U_akt)-angle(I_ges));

%Schranke_cos=0.0001;

if cos_phi>0;
    beta=pi/2;
    while (abs(cos_phi-cos_phi_aktuell)>Schranke_cos)||(abs(U_s-U_abs)>Schranke_U*U_s)
        I_ges=I_s*exp(j*beta);
        I_q=j*imag(I_s*exp(j*beta));
        I_d=real(I_s*exp(j*beta));

        XqhIsq=j*omega*(L_q-L_so)*I_q;
        XdhIsd=j*omega*(L_d-L_so)*I_d;
        XsoIs=j*omega*L_so*I_ges;
        RsIs=R_s*I_ges;   

        U_akt=j*U_p+XqhIsq+XdhIsd+XsoIs+RsIs;
        U_abs=abs(U_akt);
        cos_phi_aktuell=cos(angle(U_akt)-angle(I_ges));
        beta=beta+0.0005*pi/180;
        if beta>(2*pi+pi/2)
            return;
        end
    %     disp(beta*180/pi)
    %     disp(cos_phi)
    %     disp(cos_phi_aktuell)
    %     disp(cos_phi-cos_phi_aktuell)
    end
       
else
    beta=-pi/2;
    while (abs(cos_phi-cos_phi_aktuell)>Schranke_cos)||(abs(U_s-U_abs)>Schranke_U*U_s)
        I_ges=I_s*exp(j*beta);
        I_q=j*imag(I_s*exp(j*beta));
        I_d=real(I_s*exp(j*beta));

        XqhIsq=j*omega*(L_q-L_so)*I_q;
        XdhIsd=j*omega*(L_d-L_so)*I_d;
        XsoIs=j*omega*L_so*I_ges;
        RsIs=R_s*I_ges;   

        U_akt=j*U_p+XqhIsq+XdhIsd+XsoIs+RsIs;
        U_abs=abs(U_akt);
        cos_phi_aktuell=cos(angle(U_akt)-angle(I_ges));
        beta=beta-0.0005*pi/180;
        if beta<(-2*pi-pi/2)
            return;
        end
    %     disp(beta*180/pi)
    %     disp(cos_phi)
    %     disp(cos_phi_aktuell)
    %     disp(cos_phi-cos_phi_aktuell)
    end
end

end

